#include "robotcontroldlg.h"
#include <QMessageBox>

RobotControlDlg::RobotControlDlg(QWidget* parent)
	: QDialog(parent)
{
	ui.setupUi(this);
}

RobotControlDlg::~RobotControlDlg()
{}

bool RobotControlDlg::Init(Robot* rob)
{
	m_rob = rob;
	if (m_rob == nullptr) {
		return false;
	}

	return true;
}


void RobotControlDlg::on_pushButton_move_clicked()
{
	auto ans = QMessageBox::question(this, "警告", "执行命令后，机械手会沿直线运动到目标点，请确认路径范围内无障碍物");
	if (ans == QMessageBox::StandardButton::No) {
		return;
	}

	QList<MoveJLInfo> moves;
	MoveJLInfo move = {};
	move.acc = 20;
	move.id = 2;
	move.di = -1;
	move.vel = 20;
	move.pointType = 2;
	move.zone = 10;
	move.name = "123";
	move.pos.x = ui.x->value();
	move.pos.y = ui.y->value();
	move.pos.z = ui.z->value();
	move.pos.a = ui.a->value();
	move.pos.b = ui.b->value();
	move.pos.c = ui.c->value();
	MoveJLInfo move2 = {};
	move2.acc = 20;
	move2.id = 1;
	move2.di = -1;
	move2.vel = 20;
	move2.pointType = 2;
	move2.zone = 10;
	move2.name = "1";
	move2.pos = m_rob->GetRobotCurrentPos();
	moves.append(move2);
	moves.append(move);
	auto ret = m_rob->MoveJL(moves);
	if (ret == false) {
		QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
	}
}

void RobotControlDlg::on_pushButton_grip_clicked()
{
	int index = ui.spinBox->value();
	auto ret = m_rob->SetGrip(index);
	if (ret == false) {
		QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
	}
}

void RobotControlDlg::on_pushButton_do_clicked()
{
	int index = ui.spinBox_2->value();
	int vel = ui.spinBox_3->value();

	auto ret = m_rob->SetDO(index, vel);
	if (ret == false) {
		QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
	}
}

void RobotControlDlg::on_pushButton_pause_clicked()
{
	auto ret = m_rob->Pause();
	if (ret == false) {
		QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
	}
}

void RobotControlDlg::on_pushButton_resum_clicked()
{
	auto ret = m_rob->Resum();
	if (ret == false) {
		QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
	}
}

void RobotControlDlg::on_pushButton_stop_clicked()
{
	auto ret = m_rob->Stop();
	if (ret == false) {
		QMessageBox::warning(this, "ERROR", "命令下发失败，请检查机械手状态");
	}

}
